package cn.edu.ujs.coor.common.quartz;

import cn.edu.ujs.coor.model.Project;
import cn.edu.ujs.coor.repository.relation.GroupUserRelationRepository;
import cn.edu.ujs.coor.repository.relation.ProjectGroupRelationRepository;
import cn.edu.ujs.coor.repository.relation.ProjectUserRelationRepository;
import cn.edu.ujs.coor.service.MessageService;
import cn.edu.ujs.coor.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
@Configurable
@EnableScheduling
public class ScheduledTask {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectUserRelationRepository projectUserRelationRepository;

    @Autowired
    private ProjectGroupRelationRepository projectGroupRelationRepository;

    @Autowired
    private GroupUserRelationRepository groupUserRelationRepository;

    @Autowired
    private MessageService messageService;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Scheduled(cron = "0 0 6 * * *")
    public void projectSchedued () {
        // 明天完结项目
        List<Project> list1 = projectService.findNearDeadline(1);
        for (Project project : list1) {
            // 协同消息提示
            messageService.add("协同项目【截止】提示",
                    String.format("您参与的协同项目【%s】明日【%s】截止，请注意相关信息！", project.getName(), sdf.format(project.getDeadline())), findids(project.getId()));
            // 负责人提示
            messageService.add("协同项目【截止】提示",
                    String.format("您创建的协同项目【%s】明日【%s】截止，请注意相关进度！", project.getName(), sdf.format(project.getDeadline())), project.getUserId());
        }
        // 今天完结项目
        List<Project> list2 = projectService.findNearDeadline(0);
        for (Project project : list2) {
            // 协同消息提示
            messageService.add("协同项目【截止】提示",
                    String.format("您参与的协同项目【%s】今日【%s】截止，请注意相关信息！", project.getName(), sdf.format(project.getDeadline())), findids(project.getId()));
            // 负责人提示
            messageService.add("协同项目【截止】提示",
                    String.format("您创建的协同项目【%s】今日【%s】截止，请注意相关进度！", project.getName(), sdf.format(project.getDeadline())), project.getUserId());
        }
        // 自动完结
        List<Project> list3 = projectService.findDeadlined();
        for (Project project : list3) {
            project.setStatus("1");
            projectService.save(project);
            // 协同消息提示
            messageService.add("协同项目【截止】提示",
                    String.format("您参与的协同项目【%s】超出截止日期【%s】，自动完结，请注意相关信息！", project.getName(), sdf.format(project.getDeadline())), findids(project.getId()));
            // 负责人提示
            messageService.add("协同项目【截止】提示",
                    String.format("您创建的协同项目【%s】超出截止日期【%s】，自动完结，请注意相关信息！", project.getName(), sdf.format(project.getDeadline())), project.getUserId());
        }
    }

    private List<String> findids (String projectId) {
        Set<String> ids = new HashSet<>();
        ids.addAll(projectUserRelationRepository.findUserByProjectId(projectId));
        for (String groupId : projectGroupRelationRepository.findGroupByProjectId(projectId)) {
            ids.addAll(groupUserRelationRepository.findByGroupId(groupId));
        }
        return new ArrayList<>(ids);
    }
}
